//
//  PersonDetailTableViewController.m
//  Presence
//
//  Created by mstevens on 11/6/09.
//  Copyright 2009 __MyCompanyName__. All rights reserved.
//

#import "PersonDetailTableViewController.h"
#import "TwitterHelper.h"


@implementation PersonDetailTableViewController

@synthesize person;

- (id)initWithStyle:(UITableViewStyle)style andPerson:(PersonModel *)p {
    // Override initWithStyle: if you create the controller programmatically and want to perform customization that is not appropriate for viewDidLoad.
    if (self = [super initWithStyle:style]) {
		[self setPerson:p];
		self.title = person.displayName;
		// Fetch status list
		NSArray *feed = [TwitterHelper fetchTimelineForUsername:person.userName];
		NSUInteger i, count = [feed count];
		NSMutableArray *stats = [[NSMutableArray alloc] initWithCapacity:count];
		for (i = 0; i < count; i++) {
			NSObject * status = [((NSDictionary *)[feed objectAtIndex:i]) valueForKey:@"text"];
			[stats addObject:status];
		}
		[person setStatuses:stats];
		[[self tableView] setAllowsSelection:NO];
    }
    return self;
}

/*
- (void)viewDidLoad {
    [super viewDidLoad];

    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
}
*/

/*
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
}
*/
/*
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
}
*/
/*
- (void)viewWillDisappear:(BOOL)animated {
	[super viewWillDisappear:animated];
}
*/
/*
- (void)viewDidDisappear:(BOOL)animated {
	[super viewDidDisappear:animated];
}
*/

/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
	// Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
	
	// Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
	// Release any retained subviews of the main view.
	// e.g. self.myOutlet = nil;
}


#pragma mark Table view methods

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}


// Customize the number of rows in the table view.
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
	if (section == 0) {
		// First section contains description
		return 1;
	} else {
		return [[person statuses] count];
	}
}


- (UIFont *) getMyFont {
  UIFont *myFont = [UIFont systemFontOfSize:12.0];
  return myFont;
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    NSString *CellIdentifier = [NSString stringWithFormat:@"Cell%d%d", indexPath.section, indexPath.row];
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
    }
    
    // Set up the cell...
	if (indexPath.section == 0) {
		// Description
		NSString *descText = person.descriptionText;
		if (descText == nil || [descText isEqualToString:@""]) {
			descText = @"There's really not much to say about me.";
		}
		CGSize boundingSize = CGSizeMake(250.0, CGFLOAT_MAX);
		CGSize textSize = [descText sizeWithFont:[UIFont systemFontOfSize:18.0]
											constrainedToSize:boundingSize
												lineBreakMode:UILineBreakModeWordWrap];
		CGRect frame = CGRectMake(10,10, 260.0, textSize.height);
		UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
		[myLabel setLineBreakMode:UILineBreakModeWordWrap];
		myLabel.numberOfLines = 0;
		myLabel.text = descText;
		myLabel.textAlignment = UITextAlignmentCenter;
		[cell.contentView addSubview:myLabel];
		[cell.contentView setNeedsDisplay];
		[myLabel release];
	} else {
		// Status
		int row = indexPath.row;
		if ([person.statuses count] > row) {
			CGSize textSize = [self textSizeAtRow:row];
			CGRect frame = CGRectMake(10,10, 260.0, textSize.height);
			UILabel *myLabel = [[UILabel alloc] initWithFrame:frame];
			myLabel.font = [self getMyFont];
			[myLabel setLineBreakMode:UILineBreakModeWordWrap];
			myLabel.numberOfLines = 0;
			myLabel.text = [self textAtRow:row];
			[cell.contentView addSubview:myLabel];
			[cell.contentView setNeedsDisplay];
			[myLabel release];
		}
	}
	
    return cell;
}

- (NSString *) textAtRow: (int) row  {
	if ([person.statuses count] > row) {
		return ((NSString *)[person.statuses objectAtIndex:row]);
	}
	return nil;
}
- (CGSize) textSizeAtRow: (int) row  {
	if ([person.statuses count] > row) {
		CGSize boundingSize = CGSizeMake(250.0, CGFLOAT_MAX);
		CGSize textSize = [[self textAtRow: row] sizeWithFont:[self getMyFont]
											constrainedToSize:boundingSize
												lineBreakMode:UILineBreakModeWordWrap];
		return textSize;
	}
	return CGSizeMake(0.0, 0.0);
	
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
	if (indexPath.section == 0) {
		return 100.0;
	}
	int row = indexPath.row;
	if ([person.statuses count] > row) {
		CGSize textSize;
		textSize = [self textSizeAtRow: row];

		NSLog(@"textSize: width=%f, height=%f",
			  textSize.width, textSize.height);
		return textSize.height + 20;
	}
	return 80.0;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
	if (section == 0) {
		return @"About Me";
	} else {
		return @"What I'm doing";
	}
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // Navigation logic may go here. Create and push another view controller.
	// AnotherViewController *anotherViewController = [[AnotherViewController alloc] initWithNibName:@"AnotherView" bundle:nil];
	// [self.navigationController pushViewController:anotherViewController];
	// [anotherViewController release];
}


/*
// Override to support conditional editing of the table view.
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the specified item to be editable.
    return YES;
}
*/


/*
// Override to support editing the table view.
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        // Delete the row from the data source
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:YES];
    }   
    else if (editingStyle == UITableViewCellEditingStyleInsert) {
        // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
    }   
}
*/


/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath *)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath {
}
*/


/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView canMoveRowAtIndexPath:(NSIndexPath *)indexPath {
    // Return NO if you do not want the item to be re-orderable.
    return YES;
}
*/


- (void)dealloc {
	[person release];
    [super dealloc];
}


@end

